package com.jiulan.hearthearing.ui.course.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseModel;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.course.adapter.CourseDetailsHudongAdapter;
import com.jiulan.hearthearing.ui.course.model.CourseDetailsHudongModel;
import com.jiulan.hearthearing.widget.SpaceVerticalItemDecoration;

import java.util.HashMap;
import java.util.Map;

public class CourseDetailsHudongFragment extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private int id;
    private CourseDetailsHudongModel model;
    private CourseDetailsHudongAdapter adapter;

    private TextView tvCommentNum;
    private RecyclerView rcvComment;
    private EditText etSay;
    private LinearLayout llShare;
    private LinearLayout llCollect;
    private LinearLayout llHuancun;

    public static CourseDetailsHudongFragment newInstance(int id) {
        Bundle args = new Bundle();
        args.putInt("id", id);
        CourseDetailsHudongFragment pageFragment = new CourseDetailsHudongFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getInt("id");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_course_details_hudong, container, false);
        context = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        tvCommentNum = view.findViewById(R.id.tvCommentNum);
        rcvComment = view.findViewById(R.id.rcvComment);
        etSay = view.findViewById(R.id.etSay);
        llShare = view.findViewById(R.id.llShare);
        llCollect = view.findViewById(R.id.llCollect);
        llHuancun = view.findViewById(R.id.llHuancun);
        llShare.setOnClickListener(this);
        llCollect.setOnClickListener(this);
        llHuancun.setOnClickListener(this);
        rcvComment.setLayoutManager(new LinearLayoutManager(context));
        rcvComment.addItemDecoration(new SpaceVerticalItemDecoration(context, R.dimen.dp_12));
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            getData();
        }
    }

    private void getData() {
        new HttpRequest(context).doGet(UrlConstants.course_comment_list + String.valueOf(id), "", new HashMap(), CourseDetailsHudongModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CourseDetailsHudongModel) {
                    model = (CourseDetailsHudongModel) result;
                    setView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setView() {
        tvCommentNum.setText("(" + String.valueOf(model.getData().size()) + ")");
        adapter = new CourseDetailsHudongAdapter(model.getData());
        rcvComment.setAdapter(adapter);
    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llShare:
                break;
            case R.id.llCollect:
                collect();
                break;
            case R.id.llHuancun:
                break;
        }
    }

    private void collect() {
        Map<String, String> map = new HashMap<>();
        map.put("id", String.valueOf(id));
        map.put("type", "video");
        new HttpRequest(context).doPost(UrlConstants.collection_add, "", map, BaseModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof BaseModel) {
                    BaseModel baseModel = (BaseModel) result;
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }
}
